package com.monster.handler;

import com.monster.exception.AgeException;
import com.monster.exception.NameException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

/**
 * description:
 * 全局异常处理
 * @author Yuanml on 2021/1/5 22:42
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(NameException.class)
    public ModelAndView  handleNameException(Exception e) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("msg", "name只能是zs");
        modelAndView.addObject("ex", e);
        modelAndView.setViewName("nameError");
        return modelAndView;
    }

    @ExceptionHandler(AgeException.class)
    public ModelAndView handleAgeException(Exception e) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("msg", "age必须小于80");
        modelAndView.addObject("ex", e);
        modelAndView.setViewName("ageError");
        return modelAndView;
    }

    @ExceptionHandler
    public ModelAndView handleOtherException(Exception e) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("msg", "系统内部错误");
        modelAndView.addObject("ex", e);
        modelAndView.setViewName("error");
        return modelAndView;
    }
}
